﻿#define _STR_MODULE_

#include "stm32f4xx_hal.h"
#include "cmsis_os.h"
#include "strctr.h"
#include "usb_host.h"
#include "pincfg.h"
#include "ff.h"
#include "fatfs.h"
#include "os_obj.h"
#include "common.h"
#include "udisk_task.h"
#include "bsp_hal.h"

#include "./pdf/demo.h"

extern USBH_HandleTypeDef hUsbHostHS;
extern uint32_t uwTick;

FATFS FatFs[1]; /* File system object for logical drive */
FRESULT FsSt, FatSt;
FIL fil[2];
DIR fildir;

void UsbMassInit(void);
void FillRwTest(void);
void FlashQ25CmdHandle(void);
void udisk_fx_file_tst(void);
void UsbFoundHandle(void);

#define BUFF_SIZE      4096
uint8_t * fill_buff;

/*****************************************************************************//*!
*
* @brief   led task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void StorageTaskEntry(void const * argument)
{
	uint32_t UsbFoundTimeCnt;
	osDelay(ms_ticks(1000));

	/* init code for FATFS */
	MX_FATFS_Init();

	/* init code for USB_HOST */
	MX_USB_HOST_Init();

	for (;;)
	{
		osDelay(1);

		USBH_Process((USBH_HandleTypeDef *)&hUsbHostHS);

		if (HOST_CLASS == hUsbHostHS.gState)
		{
			if (UsbFoundTimeCnt == ms_ticks(2000))
			{

				fill_buff = mem_malloc(BUFF_SIZE);

				FillRwTest();

				// udisk_fx_file_tst();

				mem_free(fill_buff);
			}
			else
			{

			}

		}
		else
		{
			UsbFoundTimeCnt = 0;

		}

		UsbFoundTimeCnt++;
	}
}

/*****************************************************************************//*!
*
* @brief   file read and write test.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void FillRwTest(void)
{
	UINT FsResdLen;
	uint32_t out_val, len, cnt, index;
	volatile uint32_t  uTickStart;
	CHAR  * local_buffer = mem_malloc(10 * 1024);

	dbg_puts("===============================================================\n");
	dbg_puts("Udisk FatFs mount !\n");

	f_mount(&FatFs[0], "1:", 1);

	do
	{
		if (FR_OK != FsSt)
		{
			dbg_puts("FATFS mount fail !\n");
			f_mount(NULL, "1:", 0);
			break;
		}

		FsSt = f_open(&fil[0], "1:/CAN.C", FA_READ);
		if (FsSt == FR_OK)
		{

			FsResdLen = 1024;
			FsSt = f_lseek(&fil[0], 0);

			FsSt = f_read(&fil[0], &fill_buff[0], 4096, &FsResdLen);

			f_close(&fil[0]);
		}
		else
		{
			dbg_puts("Open CAN.C fail !\n");
		}

		uTickStart = uwTick;;
		
		//写入文件测试
		FsSt = f_open(&fil[0], "1:/demo.pdf", FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
		author_demo_api();
		f_close(&fil[0]);
		
		//写入文件测试
		FsSt = f_open(&fil[0], "1:/explor.csv", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);

		if (FsSt == FR_OK)
		{
			dbg_puts("Open explor.csv sucess !\n");
			FsSt = f_lseek(&fil[0], f_size(&fil[0]));
		}
		else
		{
			dbg_puts("Open explor.csv fail !\n");
		}

		memset(fill_buff, ' ', BUFF_SIZE);

		if (FsSt == FR_OK)
		{
			dbg_puts("Write explor.csv start !\n");

			out_val = 0;

			udisk_st.fil_write_time = app_tick_get();

			for (index = 0; index < 4; index++)
			{
				len = 0;

				for (cnt = 0; cnt < 4 * 3; cnt++)
				{
					i32_to_string(&local_buffer[len], out_val++, 0x63, 0x10);
					len += 10;
					local_buffer[len++] = ',';
					local_buffer[len++] = ' ';

					if (0x0F == (cnt & 0x0F))
					{
						local_buffer[len++] = '<';
						local_buffer[len++] = '>';
						local_buffer[len++] = '\r';
						local_buffer[len++] = '\n';
					}
				}
				FsSt = f_write(&fil[0], local_buffer, len, &FsResdLen);
			}

			udisk_st.fil_write_time = app_tick_get() - udisk_st.fil_write_time;
			udisk_st.file_write_size = index * (cnt * 12 + 2);
		}

		f_close(&fil[0]);
	} while (0);

	f_mount(NULL, "1:", 0);

	mem_free(local_buffer);

	dbg_puts("FatFs unmount !\n");
}


int spdf_disk_out(uint8_t *buf, int len)
{
	UINT FsResdLen;
	
	f_write(&fil[0], buf, len, &FsResdLen);
	
	return len;
}
